//
//  XCRefreshView.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/7.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit

class XCRefreshView: UIView {

  /// 显示加载的旋转图片
    @IBOutlet weak var LoadIcon: UIImageView!
    /// 带箭头的View
    @IBOutlet weak var TipView: XCRefreshView!
    /// 箭头图片
    @IBOutlet weak var TipIco: UIImageView!
    
    class func refreshView() -> XCRefreshView {
     return NSBundle.mainBundle().loadNibNamed("XCRefreshView", owner: nil, options: nil).last as! XCRefreshView
    }
    
    func rotationTipIcon(isUp:Bool) {
        UIView.animateWithDuration(0.25) { () -> Void in
            self.TipIco.transform = isUp ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.0001)) : CGAffineTransformIdentity
        }
    }
    

    /// 开始动画
    func startRotationAnim (){
        /// 隐藏箭头向下的View
       
        LoadIcon.hidden = false
    let  RotationKey = "RotationKey"
        if let _ = LoadIcon.layer.animationForKey(RotationKey){
            print("已经添加了动画")
            self.layer.removeAllAnimations()
            return
        }
         TipView.hidden = true
        let rotationAnim = CABasicAnimation(keyPath: "transform.rotation")
    
        rotationAnim.duration = 0.25
     
        rotationAnim.repeatCount = MAXFLOAT
        rotationAnim.toValue = 2 * M_PI
           rotationAnim.removedOnCompletion = false
        LoadIcon.layer.addAnimation(rotationAnim, forKey: RotationKey)
    }
    
    /// 停止动画
    func StopAnim(){

        LoadIcon.layer.removeAllAnimations()
        print("结束动画")
        /// 显示上面的tipView
        LoadIcon.hidden = true
        TipView.hidden = false


    }

}
